<?php

/**
 * Created by ymk
 * User: Administrator
 * Date: 2018/6/25
 * Time: 11:01
 */

namespace MeetUp\Model;


class Topic extends Base
{
    protected $table = 'topic';
    protected $primaryKey = 'topic_id';
    protected $fillable = ['uid','topic_name','topic_img','details','topic_groupid','close','is_hot'];
    //热门帖子
    public function topic(){
        return $this->where(['is_hot'=>1,'close'=>1])->orderBy('updated_at','desc')->take(3)->get()->toArray();
    }
    //关联users
    public function user(){
        return $this->hasOne('MeetUp\Model\Users','id','uid');
    }
    //关联组group
    public function group(){
        return $this->hasOne('MeetUp\Model\Group','group_id','topic_groupid');
    }
    //帖子的详情
    public function topic_details($data,$url){
        $TopicComment = new TopicComment();
        $TopicLike = new TopicLike();
        foreach ($data as $key => $value){
            //发帖人信息
            $userinfo = Topic::find($value['topic_id'])->user()->get()->take(5)->toArray();
            $data[$key]['user_id'] = $userinfo[0]['id'];
            $data[$key]['user_name'] = $userinfo[0]['name'];
            $data[$key]['thumimg'] = $userinfo[0]['thumimg'];
            //评论数量
            $data[$key]['comment_count'] = $TopicComment->number($value['topic_id']);
            //点赞数量
            $data[$key]['topic_count'] = $TopicLike->number($value['topic_id']);
            //距发帖的时间差(--分钟前)
            $data[$key]['time'] = $this->formatDate($value['created_at']);
            //图片路径转换
            $data[$key]['topic_img'] = $url.$value['topic_img'];
        }
        return $data;
    }


}